package servlet;

import bean.User;
import dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "RegisterServlet", value = "/RegisterServlet")
public class RegisterServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
//设置编码格式
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
//获取前端传来参数
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
//创建空 user 对象，填充参数数据并传入 dao 层
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setEmail(email);
        UserDao userDao = new UserDao();
        boolean flag = userDao.insert(user);
//判断注册是否成功
        if (flag) {
            out.println("注册成功，欢迎 " + name);
            String url ="/liuyutaoTest3_war_exploded/login.jsp";
            out.println("注册成功，若想登录，请访问：<a href = '" + url +  "'>登录页面</a>");
        } else {
            out.println("注册失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
        this.doGet(request, response);
    }

}
